﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Dal
{
    /// <summary>
    /// 杨昊 2011-11-6
    /// </summary>
    public class DbSqlHelper
    {
        static string connstr = System.Configuration.ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;//得到web.config中配置的文件
        //执行sql语句,返回数据集
        public static DataSet Query(string sql)
        {
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
        }
        //执行SQL命令
        public static int ExecuteSql(string sql)
        {
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = sql;
                conn.Open();
                int ret = cmd.ExecuteNonQuery();
                return ret;
            }

        }
        public static int LoginSql(string sql)
        {
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                conn.Open();
                SqlCommand cmd=new SqlCommand (sql,conn);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
        }
        public static string UserNameSql(string sql,string str)
        {
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader dr = cmd.ExecuteReader();
                dr.Read();
                return dr[str].ToString();
            }
        }
    }
}
